﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smartec.Accounts.Camel.AMS.Model.Entities;
using Smartec.Accounts.Camel.AMS.Model.Repository;

namespace Smartec.Accounts.Camel.AMS.Model.Managers
{
    internal static class AdministrationManager
    {
        #region Authorization
        public static bool AuthorizeAction(string controllerName, string actionName, string[] roles)
        {
            bool authorized = false;
            bool view = false;
            bool add = false;
            bool edit = false;
            bool delete = false;

            foreach (string role in roles)
            {
                if (role == "Administrator")
                {
                    view = true;
                    add = true;
                    edit = true;
                    delete = true;
                    break;
                }

                RoleController roleController = RoleControllerRepository.GetRoleController(controllerName, role);
                if (roleController != null)
                {
                    if (!view) view = roleController.View;
                    if (!add) add = roleController.Add;
                    if (!edit) edit = roleController.Edit;
                    if (!delete) delete = roleController.Delete;
                }
            }

            switch (actionName)
            {
                case "View":
                    authorized = view;
                    break;
                case "Add":
                    authorized = add;
                    break;
                case "Edit":
                    authorized = edit;
                    break;
                case "Delete":
                    authorized = delete;
                    break;
            }

            return authorized;
        }
        #endregion //Authorization
    }
}
